package net.cyue.android.js;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.dialogs.TipDialog;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class AndroidJSInterface
{
    private final Context context;

    public AndroidJSInterface(Context context)
    {
        this.context = context;
    }

    @JavascriptInterface
    public String getVersion()
    {
        try {
            PackageInfo packageInfo = this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            PopTip.show(e.getMessage());
            return null;
        }
    }


    @JavascriptInterface
    public void toast(String text)
    {
        Toast
            .makeText(this.context, text, Toast.LENGTH_SHORT)
            .show();
    }
    @JavascriptInterface
    public void toast(String text, String duration)
    {
        int d = Toast.LENGTH_SHORT;
        if (duration.equals("long") || duration.equals("LONG"))
            d = Toast.LENGTH_LONG;
        Toast
            .makeText(this.context, text, d)
            .show();
    }
    @JavascriptInterface
    public void toastLong(String text)
    {
        Toast
            .makeText(this.context, text, Toast.LENGTH_LONG)
            .show();
    }


    @JavascriptInterface
    public String getCacheDirPath()
    {
        return context.getExternalCacheDir().getAbsolutePath();
    }
    @JavascriptInterface
    public String getSdcardPath()
    {
        return Environment.getExternalStorageDirectory().getPath();
    }
    @JavascriptInterface
    public String getSdcardDirPath()
    {
        return Environment.getExternalStorageDirectory().getPath();
    }


    @JavascriptInterface
    public void writeTextFile(String savePath, String content)
    {
        try {
            OutputStream out = new FileOutputStream(savePath);
            out.write(content.getBytes());;
            out.flush();
            out.close();
            PopTip.show("写出成功");
        } catch (IOException e) {
            TipDialog.show(e.getMessage());
        }

    }


    @JavascriptInterface
    public void shareText(String text)
    {
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        // 指定发送内容的类型
        sendIntent.setType("text/plain");
        // 指定发送的内容
        sendIntent.putExtra(Intent.EXTRA_TEXT, text);
        this.context.startActivity(Intent.createChooser(sendIntent, "分享到......"));
    }


    @JavascriptInterface
    public void sendEmail(String target, String subject, String content)
    {
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/plain");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, target);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(Intent.EXTRA_TEXT, content);
        this.context.startActivity(Intent.createChooser(emailIntent, "发送邮件"));
    }



}


